<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="部门名称：">
        <el-input v-model="searchForm.deptName" size="small" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮组 -->
    <el-row>
      <el-button type="primary" size="mini" icon="el-icon-plus" plain @click="addRoleDialog">新增</el-button>
      <el-button type="danger" size="mini" icon="el-icon-delete" plain>删除</el-button>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="deptList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="角色编号" prop="id" />
      <el-table-column label="角色名称" prop="deptName" :show-overflow-tooltip="true" />
      <el-table-column label="负责人" prop="leader" :show-overflow-tooltip="true" />
      <el-table-column label="联系邮箱" prop="email" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- dialog -->
    <el-dialog
      title="添加角色"
      :visible.sync="addRoleVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="addDeptForm" label-width="80px" :model="addDeptForm" :rules="addDeptRules">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="addDeptForm.deptName" size="small" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="上级部门">
          <el-select v-model="addDeptForm.parentId" size="small" style="width:100%" placeholder="请选择活动区域">
            <el-option label="顶级" value="0" />
            <el-option v-for="dept,index in deptList" :key="index" :label="dept.deptName" :value="dept.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="addDeptForm.leader" size="small" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addDeptForm.email" size="small" placeholder="请输入角色名称" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="addRoleHandler">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog
      title="编辑角色"
      :visible.sync="editRoleVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="addDeptForm" label-width="80px" :model="addDeptForm" :rules="addDeptRules">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="addDeptForm.deptName" size="small" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="上级部门">
          <el-select v-model="addDeptForm.parentId" size="small" style="width:100%" placeholder="请选择活动区域">
            <el-option label="顶级" value="0" />
            <el-option v-for="dept,index in deptList" :key="index" :label="dept.deptName" :value="dept.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="addDeptForm.leader" size="small" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addDeptForm.email" size="small" placeholder="请输入角色名称" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="editHandler">修 改</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { queryDept, delDept, addDept, updateDept } from '@/api/dept'

export default {
  name: 'Index',
  data() {
    return {
      // 搜索条件
      searchForm: {
        deptName: ''
      },
      // 表单数据
      deptList: [],
      loading: true,
      // 分页条件
      page: 1,
      limit: 5,
      total: 0,
      // 添加角色的dialog
      addRoleVisible: false,
      // 编辑角色的dialog
      editRoleVisible: false,
      // 添加角色的表单数据
      addDeptForm: {
        deptName: '',
        parentId: '0',
        leader: '',
        email: '',
        id: 0
      },
      // 添加角色的表单验证规则
      addDeptRules: {
        deptName: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' }
        ],
        leader: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.queryDeptData()
  },
  methods: {
    // 搜索
    onSubmit() {
      this.queryDeptData()
    },
    // 打开添加角色的dialog
    addRoleDialog() {
      this.addRoleVisible = true
    },
    // 添加角色的按钮点击事件
    addRoleHandler() {
      this.$refs['addDeptForm'].validate((valid) => {
        if (valid) {
          addDept(this.addDeptForm).then(res => {
            const { msg } = res
            this.$message({ type: 'success', message: msg })
            // 刷新数据
            this.queryDeptData()
            // 关闭dialog
            this.addRoleVisible = false
            // 刷新表单
            this.$refs['addDeptForm'].resetFields()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 查询用户列表
    queryDeptData() {
      queryDept({ page: this.page, limit: this.limit, deptName: this.searchForm.deptName }).then(res => {
        const { data } = res
        this.deptList = data.list
        this.total = data.total
        this.loading = false
      })
    },
    handleDelete(data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDept(data.id).then(res => {
          console.log(res)
          this.queryDeptData()
          this.$message({
            type: 'success',
            message: res.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      this.limit = val
      this.queryDeptData()
    },
    // 前去第几页
    handleCurrentChange(val) {
      this.page = val
      this.queryDeptData()
    },
    // 编辑用户按钮
    handleEdit(data) {
      this.editRoleVisible = true
      this.addDeptForm.deptName = data.deptName
      this.addDeptForm.parentId = data.parentId
      this.addDeptForm.leader = data.leader
      this.addDeptForm.email = data.email
      this.addDeptForm.id = data.id
    },
    // 关闭dialog
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$refs['addDeptForm'].resetFields()
          done()
        })
        .catch(_ => {})
    },
    // 编辑方法
    editHandler() {
      updateDept(this.addDeptForm).then(res => {
        const { msg } = res
        this.$message({ message: msg, type: 'success' })
        this.editRoleVisible = false
        this.queryDeptData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
